package applications.callcost.listeners;

import android.content.SharedPreferences;
import android.view.View;
import android.widget.EditText;
import applications.callcost.CallCostMain;

public class DismissListener implements View.OnClickListener{
	
	CallCostMain context;
	int id;
	EditText tx;
	
	public DismissListener (CallCostMain c, int i, EditText t){
		context=c;
		tx=t;
		id=i;
	}

	public void onClick(View v) {
		
		SharedPreferences.Editor editor=context.getEditor();
		
		int x=0;
		String str;
		
		if(id==CallCostMain.CREDIT_DIALOG){
			float f;
			try{
				str=tx.getText().toString();
				if(str==null){
					context.dismissDialog(id);
					context.showDialog(CallCostMain.EMPY_ERROR_DIALOG);
					return;
				}
				if(str.length()==0){
					context.dismissDialog(id);
					context.showDialog(CallCostMain.EMPY_ERROR_DIALOG);
					return;
				}
			    f=Float.parseFloat(str);
			}catch(NumberFormatException e){
				context.dismissDialog(id);
				context.showDialog(CallCostMain.ERROR_DIALOG);
				return;
			}
			editor.putFloat(CallCostMain.CREDIT_LEFT, f);
		}
		
		else{
		try{
			str=tx.getText().toString();
			if(str==null){
				context.dismissDialog(id);
				context.showDialog(CallCostMain.EMPY_ERROR_DIALOG);
				return;
			}
			if(str.length()==0){
				context.dismissDialog(id);
				context.showDialog(CallCostMain.EMPY_ERROR_DIALOG);
				return;
			}
		    x=Integer.parseInt(str);
		}catch(NumberFormatException e){
			context.dismissDialog(id);
			context.showDialog(CallCostMain.ERROR_DIALOG);
			return;
		}
		
		//TODO impostare i vari parametri qui
		switch(id){
		case CallCostMain.SCATTO_DIALOG:
			editor.putInt(CallCostMain.SCATTO, x);
			break;
		
		case CallCostMain.MINUTE_COST_DIALOG:
			editor.putInt(CallCostMain.MINUTE_COST, x);
			break;
		
		case CallCostMain.ADDITIONAL_COST_DIALOG:
			editor.putInt(CallCostMain.ADDITIONAL_COST, x);
			break;
			
		case CallCostMain.ADDITIONAL_TIME_DIALOG:
			editor.putInt(CallCostMain.ADDITIONAL_TIME, x);
			break;
			
		case CallCostMain.SMS_DIALOG:
			editor.putInt(CallCostMain.SMS_COST, x);
			break;
			
		case CallCostMain.MMS_DIALOG:
			editor.putInt(CallCostMain.MMS_COST, x);
			break;
			
		}
		}	
		
		editor.commit();
		context.dismissDialog(id);
		
	}

}
